#include "downloadHeaders/downloadcontrollerh/downloaditemcontroller.h"
#include "downloadHeaders/downloadmodelh/historyitemmodel.h"
#include <QWebEngineDownloadRequest>
#include <QWidget>
#include <QIcon>
#include <QWebEnginePage>
#include <QDesktopServices>
#include <QFileInfo>
downloaditemcontroller::downloaditemcontroller(QWebEngineDownloadRequest *download, QObject *parent):
    QObject(parent),
    downloadRequest(download)
{
    connect(downloadRequest, &QWebEngineDownloadRequest::stateChanged, this, &downloaditemcontroller::changeState);  
    historyItem = new historyitemmodel(this);
}
void downloaditemcontroller::initializeModel(QString downloadFileDir, QString downloadFileName, QUrl url, QDateTime curTime, qreal totalBytes)
{
    historyItem->downloadFileDir = downloadFileDir;
    historyItem->downloadFileName = downloadFileName;
    historyItem->url = url;
    historyItem->curTime = curTime;
    historyItem->totalBytes = totalBytes;
    historyItem->account = "undefined";
}
void downloaditemcontroller::insert()
{
    historyItem->insertRecord();

}
void downloaditemcontroller::changeState()
{

    switch (downloadRequest->state())
    {
    case QWebEngineDownloadRequest::DownloadCompleted:
       historyItem->currentState = historyItem->DownloadCompleted;
       historyItem->insertRecord();
        break;
    case QWebEngineDownloadRequest::DownloadCancelled:
        historyItem->currentState = historyItem->DownloadCancelled;
        historyItem->insertRecord();
        break;
    default:
        break;
    }

}
//void downloaditemcontroller::updateWidget()
//{

//}
qreal downloaditemcontroller::getTotalBytes()
{
    return this->downloadRequest->totalBytes();
}
qreal downloaditemcontroller::getReceivedBytes()
{
    return this->downloadRequest->receivedBytes();
}
QString downloaditemcontroller::setTitle()
{
    return this->downloadRequest->downloadFileName();
}
bool downloaditemcontroller::isPaused()
{
    return this->downloadRequest->isPaused();
}
QIcon downloaditemcontroller::icon()
{
    return historyItem->getIconByFileEx();
}


void downloaditemcontroller::pause()
{
    this->downloadRequest->pause();
}
void downloaditemcontroller::resume()
{
    this->downloadRequest->resume();
}
void downloaditemcontroller::cancel()
{
    this->downloadRequest->cancel();
}
void downloaditemcontroller::openfolder()
{
    //打开文件夹
    QDesktopServices::openUrl(QUrl(historyItem->downloadFileDir));
    //打开文件(将根据文件类型，以电脑系统默认打开方式打开)
    //QDesktopServices::openUrl(QUrl(downloadFileDir+downloadFileName));
}

void downloaditemcontroller::openfile()
{
    QDesktopServices::openUrl(QUrl(historyItem->downloadFileDir + historyItem->downloadFileName));
}
bool downloaditemcontroller:: fileExists()
{
    QFileInfo FileInfo1(historyItem->downloadFileDir + historyItem->downloadFileName);
    QFileInfo FileInfo2(historyItem->downloadFileDir + historyItem->downloadFileName + ".download");
    return (FileInfo1.exists() || FileInfo2.exists());
}
bool downloaditemcontroller::deletefile()
{
    QFileInfo FileInfo1(historyItem->downloadFileDir + historyItem->downloadFileName);
    if (FileInfo1.isFile())//如果是文件
    {
        QFile::remove(historyItem->downloadFileDir + historyItem->downloadFileName);
        //return true;
    }

    historyItem->updateStateToDelete();
    emit deleteClicked(this);
    return true;

}
void downloaditemcontroller::restart()
{

}

QString downloaditemcontroller::withUnit(qreal bytes)
{
    if (bytes < (1 << 10))
        return tr("%L1 B").arg(bytes);
    else if (bytes < (1 << 20))
        return tr("%L1 KiB").arg(bytes / (1 << 10), 0, 'f', 2);
    else if (bytes < (1 << 30))
        return tr("%L1 MiB").arg(bytes / (1 << 20), 0, 'f', 2);
    else
        return tr("%L1 GiB").arg(bytes / (1 << 30), 0, 'f', 2);
}


QString downloaditemcontroller::secsToTime(int s)
{
    int h=0,m=0;
    h=s/3600;
    s%=3600;
    m=s/60;
    s%=60;
    QString hour = h < 10 ? '0' + QString::number(h) : QString::number(h);
    QString min = m < 10 ? '0' + QString::number(m) : QString::number(m);
    QString sec = s < 10 ? '0' + QString::number(s) : QString::number(s);


    QString str=hour+":"+min+":"+sec;
    return str;

}
